package com.jmu.aslongastoday.controller;

import com.google.gson.Gson;
import com.jmu.aslongastoday.common.Result;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("memory")
public class MemoryController {
    @Resource
    StringRedisTemplate stringRedisTemplate;

    private final String DATA_BACKLOGS = "DATA_MEMORY:";

    @PostMapping("add")
    public Result add(@RequestParam String userName, @RequestBody String json){
        stringRedisTemplate.opsForValue().set(DATA_BACKLOGS+userName,json);
        return Result.success("");
    }

    @GetMapping("get")
    public Result get(@RequestParam String userName){
        String result = stringRedisTemplate.opsForValue().get(DATA_BACKLOGS + userName);
        Gson gson = new Gson();
        GsonJsonParser gsonJsonParser = new GsonJsonParser();
        List<Object> objects = gsonJsonParser.parseList(result);
        return Result.success(objects);
    }
}
